package org.ncibi.cytoscape.metscape.task;

import java.io.File;

import org.ncibi.cytoscape.metscape.data.Attributes;
import org.ncibi.cytoscape.metscape.data.Concept;
import org.ncibi.cytoscape.metscape.data.ConceptData;
import org.ncibi.cytoscape.metscape.file.DataFile;
import org.ncibi.cytoscape.metscape.file.TextFile;
import org.ncibi.cytoscape.metscape.plugin.MetScapePlugin;

import cytoscape.CyNetwork;
import cytoscape.Cytoscape;
import cytoscape.task.Task;
import cytoscape.task.TaskMonitor;
import cytoscape.task.ui.JTaskConfig;
import cytoscape.task.util.TaskManager;

public class SaveConceptsTask implements Task {

	private CyNetwork network;
	private File conceptFile;

	private TaskMonitor taskMonitor;
	private boolean interrupted = false;

	public static void save(CyNetwork network, File conceptFile) {
		SaveConceptsTask task = new SaveConceptsTask(network, conceptFile);
		configureAndRunTask(task);
	}
	
	public SaveConceptsTask(CyNetwork network, File conceptFile) {
		this.network = network;
		this.conceptFile = conceptFile;
	}
	
	private static void configureAndRunTask(SaveConceptsTask task) {
		// Configure JTask Dialog Pop-Up Box
		JTaskConfig jTaskConfig = new JTaskConfig();
		jTaskConfig.setOwner(Cytoscape.getDesktop());
		jTaskConfig.displayCancelButton(true);
		jTaskConfig.displayStatus(true);

		// Execute Task in New Thread; pops open JTask Dialog Box.
		TaskManager.executeTask(task, jTaskConfig);
	}

	public void setTaskMonitor(TaskMonitor monitor)
			throws IllegalThreadStateException {
		taskMonitor = monitor;
	}

	public void halt() {
		interrupted = true;
	}

	public String getTitle() {
		return "Output as File";
	}

	public void run() {
		try {
			taskMonitor.setPercentCompleted(-1);
			taskMonitor.setStatus("Building file...");
			DataFile base = new TextFile();
			base.setValue("Name",0,0);
			base.setValue("ConceptType",0,1);
			base.setValue("#Genes",0,2);
			base.setValue("Coeff",0,3);
			base.setValue("OddsRatio",0,4);
			base.setValue("P-Value",0,5);
			base.setValue("FDR",0,6);
			base.setValue("Direction",0,7);
			base.setValue("SigGenes",0,8);
			int row = 1;
			ConceptData conceptData = MetScapePlugin.getPluginData().getConceptDataStore()
				.get(Attributes.network.getIntegerAttribute(network.getIdentifier(), "ConceptData"));
			for(Concept concept: conceptData.getAllConcepts()) {
				base.setValue(concept.getConceptName(),row,0);
				base.setValue(concept.getConceptType(),row,1);
				base.setValue(concept.getNumUniqueGenes(),row,2);
				base.setValue(concept.getCoeff(),row,3);
				base.setValue(concept.getOddsRatio(),row,4);
				base.setValue(concept.getPvalue(),row,5);
				base.setValue(concept.getFdr(),row,6);
				base.setValue(concept.getDirection(),row,7);
				String geneList = concept.getGeneIds().toString();
				geneList = geneList.replace("[", "");
				geneList = geneList.replace("]", "");
				base.setValue(geneList,row,8);
				
				row++;
				
				if(interrupted)
					throw new Exception();
			}
			base.save(conceptFile);

			taskMonitor.setStatus("Complete");
			taskMonitor.setPercentCompleted(100);

		} catch (Throwable t) {
			if (!interrupted)
				taskMonitor.setException(t, "File output failed");
		}

	}
}
